﻿using System;
using System.Reflection;
using System.Text;

namespace rehot
{
    using rehot.core;
    public static class HotParam
    {
        public static object[] Read(params IHotParam[] _params)
        {
            var _ps = new object[_params.Length];
            for (int i = 0; i < _params.Length; i++) _ps[i] = _params[i].Get();
            return _ps;
        }
        internal static string ReadParamTypeSnapshot(params IHotParam[] _params)
        {
            var _snapshot = new StringBuilder("(");
            for (int i = 0; i < _params.Length; i++) _snapshot.Append(_params[i].GetTypeName()).Append(" ,");
            if (_snapshot.Length > 2) _snapshot.Remove(_snapshot.Length - 2, 2);
            return _snapshot.Append(")").ToString().ToLower();
        }

        public static HotParam<Tin> Put<Tin>(Tin param)
        {
            return new HotParam<Tin>(param);
        }
    }
    public class HotParam<Tin> : IHotParam
    {
        internal Tin param { get; set; }
        public HotParam(Tin obj)
        {
            param = obj;
        }
        public object Get()
        {
            if (param is HotObject hotobj) return hotobj._obj;
            else if (param is HotEnum hotEnum) return hotEnum.Value;
            else if (param is HotType hotType) return hotType._type;
            else if (param is HotOneDLL hotOneDLL) return hotOneDLL.assembly;
            else if (param is HotDLL hotDLL) return hotDLL.assembly;
            else if (param is IHotEvent hotEvent) return hotEvent.Get();
            else if (param is HotAttribute hotAttribute) return hotAttribute._obj;
            return param;
        }
        public string GetTypeName()
        {
            if (param is HotObject hotobj) return hotobj.Type.Name;
            else if (param is HotEnum hotEnum) return hotEnum.Type.Name;
            else if (param is HotAttribute hotAttribute) return hotAttribute.Type.Name;
            else if (param is IHotEvent hotEvent) return hotEvent.Get().GetType().Name;
            else if (param is HotType hotType) return typeof(Type).Name;
            else if (param is HotOneDLL hotOneDLL) return typeof(Assembly).Name;
            else if (param is HotDLL hotDLL) return typeof(Assembly).Name;
            return typeof(Tin).Name;
        }
        public bool IsTypeName(string typeName)
        {
            if (param is HotObject hotobj) return hotobj.Type.IsType(typeName);
            else if (param is HotEnum hotEnum) return hotEnum.Type.IsType(typeName);
            else if (param is HotAttribute hotAttribute) return hotAttribute.Type.IsType(typeName);
            else if (param is IHotEvent hotEvent) return HotType.IsType(hotEvent.Get().GetType(), typeName);
            else if (param is HotType hotType) return typeName == typeof(Type).Name;
            else if (param is HotOneDLL hotOneDLL) return typeName == typeof(Assembly).Name;
            else if (param is HotDLL hotDLL) return typeName == typeof(Assembly).Name;
            return HotType.IsType(typeof(Tin), typeName);
        }
    }
}
